package com.example.myapplication.part6;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;

public class CalculatorActivity extends AppCompatActivity {

    private EditText etDisplay;
    private double currentValue = 0;
    private String pendingOperation = "";
    private boolean isNewNumber = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);

        etDisplay = findViewById(R.id.etDisplay);
        setupNumberButtons();
        setupOperationButtons();
        setupFunctionButtons();
    }

    private void setupNumberButtons() {
        int[] numberIds = {
                R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3,
                R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7,
                R.id.btn8, R.id.btn9, R.id.btnDot
        };

        for (int id : numberIds) {
            findViewById(id).setOnClickListener(v -> {
                Button btn = (Button) v;
                handleNumberInput(btn.getText().toString());
            });
        }
    }

    private void handleNumberInput(String input) {
        String currentText = etDisplay.getText().toString();

        if (isNewNumber) {
            currentText = input.equals(".") ? "0." : input;
            isNewNumber = false;
        } else {
            if (input.equals(".") && currentText.contains(".")) return;
            currentText += input;
        }

        etDisplay.setText(currentText.replace("..", "."));
    }

    private void setupOperationButtons() {
        int[] operationIds = {
                R.id.btnAdd, R.id.btnSubtract,
                R.id.btnMultiply, R.id.btnDivide
        };

        for (int id : operationIds) {
            findViewById(id).setOnClickListener(v -> {
                Button btn = (Button) v;
                pendingOperation = btn.getText().toString();
                currentValue = Double.parseDouble(etDisplay.getText().toString());
                isNewNumber = true;
            });
        }
    }

    private void setupFunctionButtons() {
        // 清除按钮
        findViewById(R.id.btnClear).setOnClickListener(v -> {
            etDisplay.setText("0");
            currentValue = 0;
            pendingOperation = "";
            isNewNumber = true;
        });

        // 正负号按钮
        findViewById(R.id.btnSign).setOnClickListener(v -> {
            double value = Double.parseDouble(etDisplay.getText().toString());
            value *= -1;
            etDisplay.setText(formatNumber(value));
        });

        // 百分比按钮
        findViewById(R.id.btnPercent).setOnClickListener(v -> {
            double value = Double.parseDouble(etDisplay.getText().toString()) / 100;
            etDisplay.setText(formatNumber(value));
        });

        // 等号按钮
        findViewById(R.id.btnEqual).setOnClickListener(v -> {
            if (pendingOperation.isEmpty()) return;

            double secondValue = Double.parseDouble(etDisplay.getText().toString());
            try {
                switch (pendingOperation) {
                    case "+": currentValue += secondValue; break;
                    case "-": currentValue -= secondValue; break;
                    case "×": currentValue *= secondValue; break;
                    case "÷":
                        if (secondValue == 0) throw new ArithmeticException();
                        currentValue /= secondValue;
                        break;
                }
                etDisplay.setText(formatNumber(currentValue));
            } catch (ArithmeticException e) {
                etDisplay.setText("Error");
                currentValue = 0;
            }

            pendingOperation = "";
            isNewNumber = true;
        });
    }

    private String formatNumber(double value) {
        if (value == (long) value) {
            return String.valueOf((long) value);
        }
        return String.valueOf(value);
    }
}